import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router';
import App from './App.vue'
import routes from './router'
import actions from './share/action'

// 不能直接挂载 需要切换的时候 调用mount方法时候再去挂载
let history;
let router;
let app;
function render(props: any = {}) {
  if (props) {
    actions.setActions(props)
    props.onGlobalStateChange((state: any) => {
      const { userId } = state;
      // 未登录 - 返回主页
      if (!userId) {
        console.error("未检测到登录信息！");
      }
      console.log("userId5", userId);
    }, true);
  }
  history = createWebHistory('/vue');
  router = createRouter({
    history,
    routes
  });
  app = createApp(App);
  let { container } = props; // 
  app.use(router).mount(container ? container.querySelector('#vue-app') : '#vue-app')
}

// 乾坤在渲染前 给我提供了一个变量 window.__POWERED_BY_QIANKUN__

if (!window.__POWERED_BY_QIANKUN__) { // 独立运行自己
  render();

}

// 需要暴露接入协议
export async function bootstrap() {
  console.log('vue3 app bootstraped');
}

export async function mount(props: any) {
  console.log('vue3 app mount', props);
  render(props)
}
export async function unmount() {
  console.log('vue3 app unmount');
  history = null;
  app = null;
  router = null;
}